package cn.wolfcode.wolf2w.common.redis.util;

import cn.wolfcode.wolf2w.common.core.constant.SystemConstants;

import java.util.Calendar;

public enum RedisKeys {
    VERIFY_CODE(SystemConstants.VERIFY_CODE_PREFIX, SystemConstants.VERIFY_CODE_TIME * 60L),
    VERIFY_CODE_COUNTER(SystemConstants.VERIFY_CODE_COUNTER, secondsToMidnight()),
    STRATEGY_HASH(SystemConstants.STRATEGY_HASH_PREFIX, -1L),
    STRATEGY_THUMB(SystemConstants.STRATEGY_THUMB_PREFIX, -1L),
    NOTE_HASH(SystemConstants.NOTE_HASH_PREFIX, -1L),
    NOTE_THUMB(SystemConstants.NOTE_THUMB_PREFIX, -1L),
    USER_FAVOR(SystemConstants.USER_FAVOR_PREFIX, -1L),
    USER_NOTE_FAVOR(SystemConstants.USER_NOTE_FAVOR_PREFIX, -1L),
    FOLLOWS_SET("follows_set", -1L),
    FANS_SET("fans_set", -1L),
    BRUSH_PROOF("brush_proof", 60L );

    private String prefix;
    private Long time;

    private RedisKeys(String prefix, Long time) {
        this.prefix = prefix;
        this.time = time;
    }

    public static long secondsToMidnight() {
        Calendar now = Calendar.getInstance();
        Calendar midnight = Calendar.getInstance();
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 0);

        now.setTimeInMillis(System.currentTimeMillis());
        midnight.setTimeInMillis(midnight.getTimeInMillis() + (24 * 60 * 60 * 1000));

        long seconds = (midnight.getTimeInMillis() - now.getTimeInMillis()) / 1000;
//        System.out.println(now.getTimeInMillis());
//        System.out.println(midnight.getTimeInMillis());
        System.out.println(Math.max(0, seconds) + "s");
        return Math.max(0, seconds);
    }

    public String join(String...values) {
        StringBuilder sb = new StringBuilder(80);
        sb.append(this.prefix);
        for (String value : values) {
            sb.append(":").append(value);
        }
        return sb.toString();
    }

    public Long getTime() {
        return this.time;
    }

    public String getSuffix(String value){
        return value.replace(this.prefix + ":", "");
    }
}
